package com.ruoyi.cms.controller;

import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.page.PageMethod;
import com.ruoyi.cms.domain.*;
import com.ruoyi.cms.domain.vo.CmsWzkfModuleVo;
import com.ruoyi.cms.service.*;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.utils.PageUtils.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

import static com.ruoyi.common.utils.PageUtils.clearPage;
import static com.ruoyi.common.utils.PageUtils.startPage;

@Controller
public class CmsWebController {
    @Autowired
    private ICmsSiteInfoService cmsSiteInfoService;
    @Autowired
    private ICmsSiteModuleService cmsSiteModuleService;
    @Autowired
    private ICmsSiteBannerService cmsSiteBannerService;

    @Autowired
    private ICmsWzkfModuleService cmsWzkfModuleService;

    @Autowired
    private ICmsWzkfBannerService cmsWzkfBannerService;


    @Autowired
   private ICmsAppModuleService cmsAppModuleService;


   @Autowired
    private ICmsAppBannerService cmsAppBannerService;


    @Autowired
    private ICmsXcxkfModuleService cmsXcxkfModuleService;


    @Autowired
    private ICmsXcxkfBannerService cmsXcxkfBannerService;


    @Autowired
    private ICmsAlModuleService cmsAlModuleService;


    @Autowired
    private ICmsAlBannerService cmsAlBannerService;



    @Autowired
    private ICmsGywmModuleService cmsGywmModuleService;


    @Autowired
    private ICmsGywmBannerService cmsGywmBannerService;

    private CmsSiteInfo handPubData(Model model) {
        CmsSiteInfo info = cmsSiteInfoService.selectUseCmsSiteInfo();
        CmsSiteModule head = cmsSiteModuleService.selectCmsSiteModuleTypeValue(info.getModuleType(), "m_head");
        CmsSiteModule footer = cmsSiteModuleService.selectCmsSiteModuleTypeValue(info.getModuleType(), "m_footer");
        model.addAttribute("info", info);
        model.addAttribute("head", head);
        model.addAttribute("footer", footer);
        return info;
    }

    @GetMapping("/index")
    public String index(Model model) {
        //获取站点基础信息
        CmsSiteInfo info = handPubData(model);
        //封装查询条件
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        //开始查询
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort)); //排序

        //存放最终所有数据
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        //遍历大表
        for (CmsSiteModule m : modules) {
            //封装条件
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId()); //大表小表对应关系
            paramBanner.setStatus(Constants.SUCCESS);
            //开始查询大表中某条数据对应的小表数据（多个）
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort)); //给获取的数据排序
            //开始封装数据
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            //将封装好的某个大表数据放进去
            moduleInfos.add(r);
        }
        //将获取到的所有数据返回到前端
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));
        return "index";
    }

    @GetMapping("/cms_websit")
    public String websit(Model model) {
        //获取站点基础信息
        CmsSiteInfo info = handPubData(model);
        //封装查询条件
        String moduleType = info.getModuleType();
        CmsWzkfModule cmsWzkfModule = new CmsWzkfModule();
//        cmsWzkfModule.setType(moduleType);
        cmsWzkfModule.setStatus(Constants.SUCCESS);
        //开始查询
        List<CmsWzkfModule> cmsWzkfModules = cmsWzkfModuleService.selectCmsWzkfModuleList(cmsWzkfModule);
        cmsWzkfModules.sort(Comparator.comparing(CmsWzkfModule::getSort)); //排序

        //存放最终所有数据
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsWzkfModule m : cmsWzkfModules) {
            //封装条件
            CmsWzkfBanner cmsWzkfBanner = new CmsWzkfBanner();
            cmsWzkfBanner.setModuleId(m.getId()); //大表小表对应关系
            cmsWzkfBanner.setStatus(Constants.SUCCESS);
            //开始查询大表中某条数据对应的小表数据（多个）
            List<CmsWzkfBanner> banners = cmsWzkfBannerService.selectCmsWzkfBannerList(cmsWzkfBanner);
            banners.sort(Comparator.comparing(CmsWzkfBanner::getSort)); //给获取的数据排序

            //开始封装数据
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            //将封装好的某个大表数据放进去
            moduleInfos.add(r);
        }

        //将获取到的所有数据返回到前端
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));

        return "cms_websit";
    }

 @GetMapping("/cms_app")
    public String app(Model model) {

     //获取站点基础信息
     CmsSiteInfo info = handPubData(model);
     //封装查询条件
     String moduleType = info.getModuleType();
     CmsAppModule cmsAppModule = new CmsAppModule();
//        cmsWzkfModule.setType(moduleType);
     cmsAppModule.setStatus(Constants.SUCCESS);
     //开始查询
     List<CmsAppModule> cmsAppModules = cmsAppModuleService.selectCmsAppModuleList(cmsAppModule);
     cmsAppModules.sort(Comparator.comparing(CmsAppModule::getSort)); //排序

     //存放最终所有数据
     List<Map<String, Object>> moduleInfos = new ArrayList<>();
     for (CmsAppModule m : cmsAppModules) {
         //封装条件
         CmsAppBanner cmsAppBanner = new CmsAppBanner();
         cmsAppBanner.setModuleId3(m.getId3()); //大表小表对应关系
         cmsAppBanner.setStatus(Constants.SUCCESS);
         //开始查询大表中某条数据对应的小表数据（多个）
         List<CmsAppBanner> banners = cmsAppBannerService.selectCmsAppBannerList(cmsAppBanner);
         banners.sort(Comparator.comparing(CmsAppBanner::getSort)); //给获取的数据排序

         //开始封装数据
         Map<String, Object> r = new HashMap<>(3);
         r.put("type", m.getValue());
         r.put("module", m);
         r.put("banners", banners);
         //将封装好的某个大表数据放进去
         moduleInfos.add(r);
     }

     //将获取到的所有数据返回到前端
     model.addAttribute("modules", moduleInfos);
     System.out.println(JSON.toJSON(moduleInfos));

     return "cms_app";
    }

    @GetMapping("/cms_applet")
    public String applet(Model model) {
        //获取站点基础信息
        CmsSiteInfo info = handPubData(model);
        //封装查询条件
        String moduleType = info.getModuleType();
        CmsXcxkfModule cmsXcxkfModule = new CmsXcxkfModule();
//        cmsWzkfModule.setType(moduleType);
        cmsXcxkfModule.setStatus(Constants.SUCCESS);
        //开始查询
        List<CmsXcxkfModule> cmsXcxkfModules = cmsXcxkfModuleService.selectCmsXcxkfModuleList(cmsXcxkfModule);
        cmsXcxkfModules.sort(Comparator.comparing(CmsXcxkfModule::getSort)); //排序

        //存放最终所有数据
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsXcxkfModule m : cmsXcxkfModules) {
            //封装条件
            CmsXcxkfBanner cmsXcxkfBanner = new CmsXcxkfBanner();
            cmsXcxkfBanner.setModuleId4(m.getId4()); //大表小表对应关系
            cmsXcxkfBanner.setStatus(Constants.SUCCESS);
            //开始查询大表中某条数据对应的小表数据（多个）
            List<CmsXcxkfBanner> banners =  cmsXcxkfBannerService.selectCmsXcxkfBannerList(cmsXcxkfBanner);
            banners.sort(Comparator.comparing(CmsXcxkfBanner::getSort)); //给获取的数据排序

            //开始封装数据
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            //将封装好的某个大表数据放进去
            moduleInfos.add(r);
        }

        //将获取到的所有数据返回到前端
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));

        return "cms_applet";
    }

    @GetMapping("/cms_case")
    public String case1(Model model) {
        //获取站点基础信息
        CmsSiteInfo info = handPubData(model);
        //封装查询条件
        String moduleType = info.getModuleType();
        CmsAlModule cmsAlModule = new CmsAlModule();
//        cmsWzkfModule.setType(moduleType);
        cmsAlModule.setStatus(Constants.SUCCESS);
        //开始查询
        List<CmsAlModule> cmsAlModules = cmsAlModuleService.selectCmsAlModuleList(cmsAlModule);
        cmsAlModules.sort(Comparator.comparing(CmsAlModule::getSort)); //排序

        //存放最终所有数据
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsAlModule m : cmsAlModules) {
            //封装条件
            CmsAlBanner cmsAlBanner = new CmsAlBanner();
            cmsAlBanner.setModuleId5(m.getId()); //大表小表对应关系
            cmsAlBanner.setStatus(Constants.SUCCESS);
            //开始查询大表中某条数据对应的小表数据（多个）
            List<CmsAlBanner> banners =  cmsAlBannerService.selectCmsAlBannerList(cmsAlBanner);
            banners.sort(Comparator.comparing(CmsAlBanner::getSort)); //给获取的数据排序

            //开始封装数据
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            //将封装好的某个大表数据放进去
            moduleInfos.add(r);
        }

        //将获取到的所有数据返回到前端
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));

        return "cms_case";
    }

    /**
     * 异步分页结构
     * @param model
     * @return
     */
    @GetMapping("/cms_case_page")
    @ResponseBody
    public Map cmsCasePage(Model model, @RequestParam("moduleId") Long moduleId) {
        //存放最终数据的地方
        HashMap<Object, Object> map = new HashMap<>();

        //开始分页查询
        startPage();
//        startPage(Integer.parseInt(request.getParameter("pageNum")), Integer.parseInt(request.getParameter("pageSize")));
        PageUtils.orderBy("sort");
        CmsAlBanner cmsAlBanner = new CmsAlBanner();
        cmsAlBanner.setModuleId5(moduleId);
        List<CmsAlBanner> cmsAlBanners = cmsAlBannerService.selectCmsAlBannerList(cmsAlBanner);

        //往map中放数据
        map.put("data", cmsAlBanners);

        return map;
    }

    @GetMapping("/cms_about")
    public String about(Model model) {
        //获取站点基础信息
        CmsSiteInfo info = handPubData(model);
        //封装查询条件
        String moduleType = info.getModuleType();
        CmsGywmModule cmsGywmModule = new CmsGywmModule();
//        cmsWzkfModule.setType(moduleType);
        cmsGywmModule.setStatus(Constants.SUCCESS);
        //开始查询
        List<CmsGywmModule> cmsGywmModules = cmsGywmModuleService.selectCmsGywmModuleList(cmsGywmModule);
        cmsGywmModules.sort(Comparator.comparing(CmsGywmModule::getSort)); //排序

        //存放最终所有数据
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsGywmModule m : cmsGywmModules) {
            //封装条件
            CmsGywmBanner cmsGywmBanner = new CmsGywmBanner();
            cmsGywmBanner.setModuleId6(m.getId()); //大表小表对应关系
            cmsGywmBanner.setStatus(Constants.SUCCESS);
            //开始查询大表中某条数据对应的小表数据（多个）
            List<CmsGywmBanner> banners =  cmsGywmBannerService.selectCmsGywmBannerList(cmsGywmBanner);
            banners.sort(Comparator.comparing(CmsGywmBanner::getSort)); //给获取的数据排序

            //开始封装数据
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            //将封装好的某个大表数据放进去
            moduleInfos.add(r);
        }

        //将获取到的所有数据返回到前端
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));

        return "cms_about";
    }

    @GetMapping("/cms_casedetail")
    public String casedetail(Model model) {
        CmsSiteInfo info = handPubData(model);
        return "cms_casedetail";
    }




}
